package com.qm.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.User;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import com.qm.dao.UsersDAO;
import com.qm.dao.AdminStudentDAO;
import com.qm.model.ImportExcelReprotModel;
import com.qm.model.Score;
import com.qm.model.Users;
import com.qm.util.UserExcelExport;
import com.qm.util.UserExelImport;

@Service
public class AdminStudentService {
	@Autowired
	@Qualifier("adminStudentDao")
	private AdminStudentDAO adao;

	public AdminStudentDAO getAdao() {
		return adao;
	}
	public void setAdao(AdminStudentDAO adao) {
		this.adao = adao;
	}
	public int insertStudent(Users u){
		int  result=adao.insertStudent(u);
		return result;
	}
	/**
	 * showAllStudent 显示所有学生信息
	 * @return
	 */
	public ArrayList<Users> showAllStudent(){
		ArrayList<Users> students=new ArrayList<Users>(adao.showAllStudent()) ;
		return students;
	}
	/**
	 * 通过学生ID来删除该学生
	 * @param uid
	 * @return
	 */
	public int deleteStudentByUid(int uid){
		int result=adao.deleteStudentByUid(uid);
		return result;
	}
	/**
	 * 学生信息进行分页处理
	 * @param page
	 * @param count
	 * @return
	 */
    public ArrayList<Users> studentPaging(int page,int count){
    	ArrayList<Users> pageStudent=adao.studentPaging(page,count);    
    	return pageStudent;
    }
    /**
     * 学生信息条数
     * @return
     */
    public int studnetCount(){
    	int studentCount=adao.studentCount();
    	return studentCount;
    }
    /**
     * 学生成绩进行分页法处理
     * @param page
     * @param count
     * @return
     */
   public ArrayList<Score> studentScorePaging(int page,int count){
	   ArrayList<Score> studentScore=adao.studentScorePaging(page, count);
	   return studentScore;
	   
   }
   /**
    * 学生成绩人数统计
    * @return
    */
   public int studentScoreCount(){
	   int studentScoreCount=adao.studentScoreCount();
	   return studentScoreCount;
   }
   /**
    * 按课程名称和班级名称来查询学生信息分页
    * @param cocourseCode
    * @param cid
    * @param page
    * @param count
    * @return
    */
   public ArrayList<Users> selectStudentByCourse(int cocourseCode,int cid,int page,int count){
	   ArrayList<Users> studentBycourse=adao.selectStudentByCourse(cocourseCode, cid,page,count);
	   return studentBycourse;
   }
   /**
    * 按课程名称和班级名称来查询学生人数
    * @return
    */
   public int selectStudentCountByCourse(int cocourseCode,int cid)
   {
	   int result=adao.selectStudentCountByCourse(cocourseCode,cid);
	   return result;
   }
   /**
    * 按照学生id来查询学生信息
    * @param uid
    * @return
    */
   public Users selectStudentByUid(int uid){
	   Users result=adao.selectStudentByUid(uid);
	   return result;
   }
   /**
    * 修改学生信息
    * @param u
    * @return
    */
   public int changeStudent(Users u){
	   int result=adao.changeStudent(u);
	   return result;
   }
   /**
    *  按课程名称和班级名称来查询学生成绩分页
    * @param cocourseCode
    * @param cid
    * @param page
    * @param count
    * @return
    */
   public ArrayList<Score> selectStudentScoreByCourse(int cocourseCode,int cid,int page,int count)
   {
	   ArrayList<Score> result=adao.selectStudentScoreByCourse(cocourseCode, cid, page, count);
	   return result;
	   
   }
   /**
    * 按课程名称和班级名称来查询该班级或者课程代码学生人数
    * @param cocourseCode
    * @param cid
    * @return
    */
   public int selectScoreCountByCourse (int cocourseCode,int cid)
   {
	   int result=adao.selectScoreCountByCourse(cocourseCode, cid);
	   return result;
   }

	/**
	 * 导入学生信息，excel表导入
	 * @param excel
	 * @return
	 */
	public List<ImportExcelReprotModel<Users>> insertStudents(MultipartFile excel){
		List<ImportExcelReprotModel<Users>> error =new ArrayList<>();
		//ImportExcelReprotModel<Users> reprtoModel=new ImportExcelReprotModel<>();
		try {
			ArrayList<ImportExcelReprotModel<Users>> users=UserExelImport.parseUser(excel);
			//int count=0;
		
			for(int i=0;i<users.size();i++){
				
				if(users.get(i).getErrorMsg()==null||users.get(i).getErrorMsg().isEmpty()){
					adao.insertStudents(users.get(i).data);
		
				}
				else{
					//reprtoModel=users.get(i);
					error.add(users.get(i));
				}	   
			}		
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		return error;
		
	}

	/**
	 * 导出学生成绩表，excel表导出
	 * @return
	 */
	public XSSFWorkbook student_score(){
		//ArrayList<Score> score= new ArrayList<>();
		XSSFWorkbook wb=UserExcelExport.export_results(adao.student_score());
		System.out.println("===============================================score");
		System.out.println(adao.student_score());
		return wb;
	}
}
